#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

from retrying import retry
import requests
import os

@retry
def Get_PMCID_List(nameString, dateStringStart, dateStringEnd):
    url = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi'
    payload = {"db": "pmc", "mindate": dateStringStart,"maxdate":dateStringEnd,"retmode":"json","retmax":"1000000"}
    r = requests.get(url, params=payload)
    json_object=r.json()
    with open("pmcid_list_" + nameString + ".json", 'wb') as fd:
        for chunk in r.iter_content(1024*1024):
            fd.write(chunk)
    print "pmcid_list_" + nameString + " has been successfully downloaded."
    return json_object

# @retry
# def Get_PMC_Meta(filename):
#     ids = ""
#     infile = open(filename, "r")
#     for line in infile:
#         line = line.rstrip()
#         splitline = line.split("\t")
#         ids += splitline[0] + ","
#     infile.close()
#     ids = ids[:-1]
#     taskid = filename.split(".")[-2]
#     filetype = filename.split(".")[-5]
#     xml_name = "pmcmeta." + filetype + "." + NOW_DATE + "." + taskid + ".xml"
#     if os.path.exists(xml_name):
#         os.remove(xml_name)
#     url = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi'
#     payload = {"db": "pmc", "id": ids, "retmode": "xml"}
#     r = requests.get(url, params=payload)
#     with open(xml_name, 'wb') as fd:
#         for chunk in r.iter_content(1024 * 1024):
#             fd.write(chunk)
#     print "Verifying " + xml_name + " ... ",
#     if not Verify_Meta_XML(xml_name):
#         print("invalid. Try to redownload...")
#         raise IOError("Invalid xml.")
#     else:
#         print("OK.")
#         print(xml_name + " download complete.")

@retry
def Get_PMC_Meta(ids):
    xml_name = "ttttt.xml"
    url = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi'
    payload = {"db": "pmc", "id": ids, "retmode": "xml"}
    r = requests.get(url, params=payload)
    with open(xml_name, 'wb') as fd:
        for chunk in r.iter_content(1024 * 1024):
            fd.write(chunk)
    fd.close()
    print "Verifying " + xml_name + " ... ",
    if not Verify_Meta_XML(xml_name):
        print("invalid. Try to redownload...")
        raise IOError("Invalid xml.")
    else:
        print("OK.")
        print(xml_name + " download complete.")

def Verify_Meta_XML(filename):
    import commands

    cmd = "grep '</pmc-articleset>' " + filename
    result = commands.getoutput(cmd)
    if result.find("</pmc-articleset>") != -1:
        # print filename + " is valid."
        return True
    elif result.find("No such file or directory") != -1:
        # print "Cannot find "+filename+". Try to redownload..."
        return False
    else:
        # print filename + " is invalid. Try to redownload..."
        return False

if __name__ == "__main__":
    # test=Get_PMCID_List("test_pmc_meta","2016/10/30","2016/10/30")
    # print test
    Get_PMC_Meta("5071362,5069344")
    print Verify_Meta_XML("ttttt.xml")
